package org.jimmy.tang.lang;

import java.util.Arrays;

/**
 * @author jimmy
 * 
 */
public class ObjectMethod {
    public static void main(String[] args) {
        testEquals();
        testHashCode();
        testToString();
    }

    private static void testEquals() {
        Employee4 employee = new Employee4("Bod", 5000);
        Employee4 staff = employee;
        Manager4 manager = new Manager4("Bod", 8000, 6000);
        System.out.println(employee.equals(new Employee4("Bod", 5000)));// true
        System.out.println(employee.equals(staff));// true
        System.out.println(employee.equals(null));// false
        System.out.println(manager.equals(new Manager4("Bod", 8000, 6000)));// true
        System.out.println(employee.equals(manager));// false

        System.out.println(employee instanceof Employee4);// true
        System.out.println(employee instanceof Manager4);// false
        System.out.println(manager instanceof Employee4);// true

        // 考察两个数组是否相同
        System.out.println(Arrays.equals(new int[] { 1, 2, 3 }, new int[] { 1,
                2, 3 }));// true
    }

    private static void testHashCode() {
        String s1 = "Hello,world!";
        String s2 = new String("Hello,world!");
        System.out.println(s1.hashCode());// 2007142665
        System.out.println(s2.hashCode());// 2007142665

        StringBuilder sb1 = new StringBuilder("Hello,world!");
        StringBuilder sb2 = new StringBuilder(s1);
        System.out.println(sb1.hashCode());// 1291383602
        System.out.println(sb2.hashCode());// 1814462232
    }

    private static void testToString() {
        class Employee {
            private double salary;
            private String name;

            public Employee(String name, double salary) {
                this.name = name;
                this.salary = salary;
            }

            @Override
            public String toString() {
                return getClass().getName() + "[name = " + name + ", salary = "
                        + salary + "]";
            }
        }

        Employee staff = new Employee("Bob", 8000);
        System.out.println(staff.toString());
    }

}

class Employee4 {
    public Employee4(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public boolean equals(Object otherObject) {
        // 如果两个对象具有相同的引用，返回true
        if (this == otherObject) {
            return true;
        }
        // 如果otherObjext是null，返回false
        if (otherObject == null) {
            return false;
        }
        // 如果两个对象所属的类不相同，返回false
        if (this.getClass() != otherObject.getClass()) {
            return false;
        }

        // 此时，两个对象既不是null，他们所属的类也相同，都为Employee4
        Employee4 other = (Employee4) otherObject;
        return name.equals(other.name) && (salary == other.salary);

    }

    // 定义变量
    private double salary;
    private String name;
}

class Manager4 extends Employee4 {
    public Manager4(String name, double salary, double bonus) {
        super(name, salary);
        this.bonus = bonus;
    }

    @Override
    public boolean equals(Object otherObject) {
        if (!super.equals(otherObject)) {
            return false;
        }
        Manager4 other = (Manager4) otherObject;
        return bonus == other.bonus;
    }

    private double bonus;
}
